package com.huidao.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.huidao.entity.Menu;
import com.huidao.util.JDBCUtil;

public class MenuDaoImpl implements MenuDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;

	public boolean addMenu(Menu Menu) {

		String sql = "INSERT INTO Menu(MENU_NAME,MENU_URL)VALUES(?,?)";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, Menu.getMenuName());
			ps.setString(2, Menu.getMenuUrl());
			int i = ps.executeUpdate();
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public boolean delMenu(Long id) {

		String sql = "DELETE FROM Menu WHERE MENU_ID=?";

		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, id);
			int i = ps.executeUpdate();
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public boolean updateMenu(Menu Menu) {

		String sql = "UPDATE Menu SET MENU_NAME=?,MENU_URL=?"
				+ "WHERE MENU_ID=?";

		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, Menu.getMenuName());
			ps.setString(2, Menu.getMenuUrl());
			ps.setLong(3, Menu.getMenuId());
			int i = ps.executeUpdate();
			if (i > 0) {
				return true;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return false;
	}

	public List<Menu> getListPage(int currentPage, int pageSize, String namekey) {

		List<Menu> list = new ArrayList<Menu>();
		String sql = "SELECT * FROM Menu  WHERE 1=1 ";
		if (namekey != null && !namekey.trim().equals("")) {
			sql = sql + " AND MENU_NAME LIKE '%" + namekey + "%' ";
		}
		sql = sql + " LIMIT ?,?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setInt(1, (currentPage - 1) * pageSize);
			ps.setInt(2, pageSize);
			rs = ps.executeQuery();
			while (rs.next()) {
				Menu b = new Menu();
				b.setMenuId(rs.getLong("MENU_ID"));
				b.setMenuName(rs.getString("MENU_NAME"));
				b.setMenuUrl(rs.getString("MENU_URL"));
				list.add(b);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

	public int getTotalCount(String namekey) {

		String sql = "SELECT COUNT(*) FROM Menu WHERE 1=1 ";
		if (namekey != null && !namekey.trim().equals("")) {
			sql = sql + " AND MENU_NAME LIKE '%" + namekey + "%' ";
		}
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			rs = ps.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return 0;
	}

	public List<Menu> getAllList(Long roleId) {
		List<Menu> list = new ArrayList<Menu>();
		String sql = "SELECT * FROM MENU A LEFT JOIN FUNCTION B ON A.MENU_ID=B.MENU_ID AND B.ROLE_ID=?";
		try {
			conn = JDBCUtil.getConn();
			ps = conn.prepareStatement(sql);
			ps.setLong(1, roleId);
			rs = ps.executeQuery();
			while (rs.next()) {
				Menu b = new Menu();
				b.setMenuId(rs.getLong("MENU_ID"));
				b.setMenuName(rs.getString("MENU_NAME"));
				b.setMenuUrl(rs.getString("MENU_URL"));
				if (rs.getLong("FUNCTION_ID") != 0) {
					b.setIsChecked(1);
				}
				list.add(b);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JDBCUtil.free(conn, ps, rs);
		}
		return list;
	}

}
